<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
    <a-drawer
            :title="!dataForm.imgid ? '添加' : '修改'"
            :visible="visible"
            @close="cancel"
            :body-style="{ paddingBottom: '80px' }"
            :width="720"
    >
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule"  v-loading="loading" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px" style="padding: 0px 10px;margin: 0px 50px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称" size="small"></el-input>
    </el-form-item>
    <el-form-item label="模块" >
      <el-select v-model="dataForm.type" placeholder="请选择排行榜所在模块" class="selWidth" filterable>
        <el-option
          v-for="item in rankingsTypedataList"
          :label="item.name"
          :key="item.id"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>

      <el-form-item label="排序" >
        <el-input type="number" v-model="dataForm.sortnum" placeholder="排序" size="small"></el-input>
      </el-form-item>
<!--    <el-form-item label="展示图" >-->
<!--      <upload-img text="展示图" :fileListValue="dataForm.img" v-model="dataForm.img" ></upload-img>-->
<!--    </el-form-item>-->
    </el-form>
        <div :style="{position: 'absolute',right: 0,bottom: 0,width: '100%', borderTop: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',textAlign: 'right',zIndex: 1,}">
            <el-button @click="cancel()" :style="{ marginRight: '8px' }">取消</el-button>
            <el-button type="primary" @click="submit()">确定</el-button>
        </div>
  </a-drawer>
</template>

<script>
    import api from '@/api/api'
    import uploadImg from '@/components/Upload/uploadImg'
    export default {
    components: {
      uploadImg
    },
    data () {
      return {
        visible: false,
        loading:false,
        rankingsTypedataList:[],
        dataForm: {
          id: 0,
          pid: '' ,
          name: '' ,
          nameeng: '' ,
          icon: '' ,
          img: '' ,
          type: '' ,
          hottypestatus: '' ,
          hotsearchstatus: '' ,
          status: '' ,
          sortnum: '' ,
          extra: '' ,
          createtime: '' ,
          updatetime: ''
        },
        dataRule: {
          pid: [
            { required: true, message: 'pid  为必填项', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称  为必填项', trigger: 'blur' }
          ],
          nameeng: [
            { required: true, message: '英文名称  为必填项', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标  为必填项', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '展示图  为必填项', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：0，软件类型 1、 游戏1级 2、游戏2级  为必填项', trigger: 'blur' }
          ],
          hottypestatus: [
            { required: true, message: '热门分类状态：0否,1是  为必填项', trigger: 'blur' }
          ],
          hotsearchstatus: [
            { required: true, message: '热门搜索状态：0否,1是  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：0:未启用，1:启用，2、下架  为必填项', trigger: 'blur' }
          ],
          sortnum: [
            { required: true, message: '排序  为必填项', trigger: 'blur' }
          ],
          extra: [
            { required: true, message: '扩展  为必填项', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建日期  为必填项', trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: '创建日期  为必填项', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) { // 初始化表单验证规则
        this.getRankingsTypeList()
        this.dataForm.id = id || undefined
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id){
              this.loading = true
                api.post('rest/vabc/softwarerankings/info',{id:this.dataForm.id}).then(res => {
                this.dataForm = res;
            }).finally(() => {
                this.loading = false
            });
          }
        })
      },
      getRankingsTypeList () {
        // this.dataListLoading = true
        // this.dataListLoading = true
        //postData.key=postData.key.length==0?postData.key:'%'+postData.key+'%'
        api.post('rest/vabc/softwarerankings/getRankingsTypeList',{}).then(res => {
          this.rankingsTypedataList=res.datalist
          this.search()
        }).finally(() => {
          //this.dataListLoading = false
        });
      },
      // 表单数据提交
      submit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            let postData=Object.assign({},this.dataForm)
            postData.createtime=undefined
            postData.updatetime=undefined
            if (!this.dataForm.id){
                postData.id==undefined
                api.post('rest/vabc/softwarerankings/save',postData).then(res =>{
                    this.$message.success("创建成功")
                    this.reset()
                    this.visible = false
                    this.$parent.getDataList()
                }).finally(() => {
                    this.loading = false
                });
            }else{
                api.post('rest/vabc/softwarerankings/update',postData).then(res =>{
                    this.$message.success("保存成功")
                    this.reset()
                    this.visible = false
                    this.$parent.getDataList()
                }).finally(() => {
                    this.loading = false
                });
            }
          }
        })
      },
     cancel(){
        this.visible = false
        this.reset()
    },
    reset(){
        this.$refs.dataForm.resetFields()
        this.$data.dataForm=JSON.parse(JSON.stringify(this.$options.data().dataForm))
        //图片回写
        //this.$refs["childData"].fileList=[]
    }
    }
  }
</script>
